package smedi.ismedi.mobile.bff.controller.organization;

import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.organization.api.feign.RemoteProjectOrganizationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.dto.OrganizationResponse;
import smedi.ismedi.mobile.bff.infrastructure.constant.CustomError;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织信息
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"组织值列表接口"})
@RequestMapping("/mobilebff/v1/organization/picker")
@AllArgsConstructor
public class OrganizationPickerController extends BaseController {
    private final RemoteHrOrganizationFeignApi remoteHrOrganizationFeignApi;

    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;

    private final RemoteProjectOrganizationFeignApi remoteProjectOrganizationFeignApi;

    /**
     * 获取企业组织根节点+项目所属组织+项目下的所有组织
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取所有企业组织下的项目管理组织清单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/queryAllProjectManagementHROrganizationList")
    public ResponseData<List<OrganizationResponse>> getQueryOrganizationListForSafetyCheckAndDanger(@RequestParam("projectId") Long projectId) {
        List<OrganizationResponse> list = new ArrayList<>();
        // 获取企业根组织
        ResponseData<HrOrganizationResponse> root = remoteHrOrganizationFeignApi.getHrOrganizationRoot();
        if (!root.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, root.getApplication(), root.getErrorMsg().toString());
        }
        list.add(new OrganizationResponse()
                .setOrganizationId(root.getData().getHrOrganizationId())
                .setFullName(root.getData().getFullName())
                .setShortName(root.getData().getShortName())
                .setType("hr"));

        // 获取项目所属组织
        ResponseData<ProjectBasicResponse> project = remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
        if (!project.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, project.getApplication(), project.getErrorMsg().toString());
        }

        // 获取企业
        ResponseData<HrOrganizationResponse> projectBelongHrOrganization = remoteHrOrganizationFeignApi.getHrOrganizationView(project.getData().getHrOrganizationId());
        if (!projectBelongHrOrganization.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectBelongHrOrganization.getApplication(), projectBelongHrOrganization.getErrorMsg().toString());
        }
        list.add(new OrganizationResponse()
                .setOrganizationId(projectBelongHrOrganization.getData().getHrOrganizationId())
                .setFullName(projectBelongHrOrganization.getData().getFullName())
                .setShortName(projectBelongHrOrganization.getData().getShortName())
                .setType("hr"));

        // 根据项目id获取项目下的组织清单
        ResponseData<List<ProjectOrganizationResponse>> projectOrganizations = remoteProjectOrganizationFeignApi.getQueryProjectOrganizationListByProjectId(projectId);
        if (!projectOrganizations.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, projectOrganizations.getApplication(), projectOrganizations.getErrorMsg().toString());
        }
        list.addAll(projectOrganizations.getData().stream().map(projectOrganization ->
                new OrganizationResponse()
                        .setOrganizationId(projectOrganization.getProjectOrganizationId())
                        .setFullName(projectOrganization.getFullName())
                        .setShortName(projectOrganization.getShortName())
                        .setType("project")
        ).collect(Collectors.toList()));

        return new ResponseData<List<OrganizationResponse>>().success(list.stream().distinct().collect(Collectors.toList()));
    }
}
